package cc.bookcloud.tool.ocr.util;

import lombok.extern.slf4j.Slf4j;

import java.io.*;

@Slf4j
public class FileUtil {

    private static final int numOfEncAndDec = 0x128;//定义密钥
    private static int dataOfFile = 0;



    public static void encryptFile(int random,String filePath,String fileName,String finalName){
        InputStream fis = null;
        OutputStream fos = null;
        try {
            File srcFile = new File(filePath + "/" + fileName);
            //int pos = fileName.lastIndexOf(".");
            //String newFileName = fileName.substring(0,pos);
            // 保持原生的文件名称
            File outFile = new File(filePath+"/"+finalName);
            if (!srcFile.exists()) {
                System.out.println("source file not exist!");
                return;
            }

            if (!outFile.exists()) {
                System.out.println("encrypt file created");
                outFile.createNewFile();
            }
             fis = new FileInputStream(srcFile);
             fos = new FileOutputStream(outFile);

            while ((dataOfFile = fis.read()) > -1) {
                fos.write(dataOfFile ^ (numOfEncAndDec+random));
            }
        }catch (Exception e){
            log.error("加密文件发生了异常！",e);

        }finally {
            try {
                if(fis != null) {
                    fis.close();
                }
                if(fos !=null ) {
                    fos.flush();
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }


    }


    public static void decryptFile(int random,String filePath,String fileName,String finalName){
        InputStream fis = null;
        OutputStream fos = null;
        try {
            File srcFile = new File(filePath + "/" + fileName);
            //int pos = fileName.lastIndexOf(".");
            //String newFileName = fileName.substring(0,pos);
            String path = filePath+"/"+finalName;
            File outFile = new File(path);
            log.info("解密后文件名称："+path);
            if (!srcFile.exists()) {
                System.out.println("source file not exixt");
                return;
            }

            if (!outFile.exists()) {
                System.out.println("encrypt file created");
                outFile.createNewFile();
            }
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(outFile);

            while ((dataOfFile = fis.read()) > -1) {
                fos.write(dataOfFile ^ (numOfEncAndDec+random));
            }
        }catch (Exception e){
            log.error("加密文件发生了异常！",e);

        }finally {
            try {
                if(fis != null) {
                    fis.close();
                }
                if(fos !=null ) {
                    fos.flush();
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }


    }


    /**
     * 测试
     *
     * @param args
     * @throws Exception
     */
    public static void main_bak(String[] args) throws Exception {
        //File inputFile = new File("E:\\Demo\\财政国密改造方案20140111.doc");
        //File outputFile = new File("E:\\Demo\\1.doc");
        // recovery file
        //File reFile = new File("E:\\Demo\\2.doc");
        //DecFile(outputFile, reFile);
       // EncFile(inputFile, outputFile);
    }



}
